let py;
	if($.cookie("area")==2){
        py=[0,0]
    }else{
        py=[-2.782459,+3.9238951]
    }
	var page=1;
	var main;
    var data={};
    var fi=1;
    data.page=page;
    data.pagenum=10;
    laydate.render({
	  elem: '.time'
	  ,range: true,
	  done:function(value, date, endDate){
	  	arr = value.split(' - ')
	  	$("input[name='order_start']").val(arr[0])
	  	$("input[name='order_end']").val(arr[1])
	  }
	});
    $('#more').on('click',function(){
    	if($('#showmore').css('display')=='none'){
    		$('#showmore').css('display','block')
    	}else{
    		$('#showmore').css('display','none')
    	}
	});
	//获取车型
	ajax(api.carModel.typeList,{pagenum:9999},'post').done(res=>{
		var  arr=res.data;
		console.log(res)
		var options ='<option value="">全部-车型</option>';
		arr.forEach(key => {
			options += `<option {{$Think.get.car_type}==${key.car_type_id}?selected:''} value='${key.car_type_id}'>${key.car_type_name}</option>`;
		});
		$('.carType').html(options)
		form.render('select');
	})

    var where_json={};
    {if $Think.get.order_no}
		where_json.order_no=['like','%{$Think.get.order_no}%']
	{/if}
	{if $Think.get.company_name}
		where_json.company_name=['like','%{$Think.get.company_name}%']
	{/if}
	{if $Think.get.name}
		if($('.orderertype').val()==1){
			where_json.passenger_name=['like','%{$Think.get.name}%']
		}else{
			where_json.orderer=['like','%{$Think.get.name}%']
		}
	{/if}
	{if $Think.get.phone}
		where_json.passenger_phone=['like','%{$Think.get.phone}%']
	{/if}
	{if $Think.get.order_start}
		where_json.apply_time=['between time',['{$Think.get.order_start}','{$Think.get.order_end}']]
	{/if}
	{if $Think.get.destination}
	where_json.destination=['like','%{$Think.get.destination}%']
	{/if}
	{if $Think.get.car_type}
	where_json.car_type_id=['in','{$Think.get.car_type}']
	{/if}
	{if $Think.get.meter}
	if('{$Think.get.meter}'==1){
	where_json['fee.meter']=['between',[0,100]]
	}else if('{$Think.get.meter}'==2){
	where_json['fee.meter']=['between',[100,300]]
	}else if ('{$Think.get.meter}'==3){
	where_json['fee.meter']=['>','300']		
	}
	{/if}

    where_json.order_state=['in',1]
	data.where_json=JSON.stringify(where_json)
    ajaxorder(data);
	main=[[ //表头

		{field:'order_no', width:200,title: '订单号', templet: '#orderNo', fixed: 'left'}
        ,{field: 'apply_time',width:200, title: '申请时间',  sort: true}
        ,{field: 'approval_time',width:200, title: '批准时间'} 
        ,{field: 'orders_time',width:200, title: '接单时间'}
        ,{field: 'order_type2', width:100, title: '订单类型', sort: true}
        ,{field:'orderer', width:150,title:'申请人'}
        ,{field:'approval',width:150, title:'审核人'}
        ,{field:'from_addr', width:300, title:'出发地',templet: `<div>{{d.gps.to_addr?d.gps.from_addr:'-'}}</div>`}
        ,{field:'to_addr', width:300, title:'目的地',templet: `<div>{{d.gps.to_addr?d.gps.to_addr:'-'}}</div>`}
        ,{field:'use_type2',width:100, title:'用车类型'}
        ,{field:'use_explain',width:200, title:'用车说明'}
        ,{field:'order_state2', title:'订单状态'}
        ,{ title: '操作',width:150, toolbar:'#operation_all',fixed: 'right'}
    ]];

	
	//请求订单
	function ajaxorder(data){
		Load()
		ajax(api.order.list,{data},'post').done(function(res){
			
			console.log(res)
			Load(1)
			 res=orderstate(res)
					  //展示已知数据
				table.render({
				  	elem: '#order'
				    ,cellMinWidth: 80
				    ,cols: main
					,data: res.data
					,limit: data.pagenum
					,even: true
				});
				if(res.code==1){
					console.log(res)
					 if(fi==1){
						 laypage.render({
						    elem: 'page'
						    ,count: res.count
							,layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
							,theme: '#1E9FFF'
						    ,jump: function(obj, first){
						    	if(!first){
						       		page=obj.curr
			    					data.page=page
			    					pagenum=obj.limit
			    					data.pagenum=pagenum
			    					ajaxorder(data)
		    					}else{
		    						
		    					}
						    }
						  });
						  fi=2
						 }
						//订单按钮点击
						table.on('tool(order)', function(obj){
							  var data1 = obj.data; //获得当前行数据
							  var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
							  var tr = obj.tr; //获得当前行 tr 的DOM对象
							 
							  if(layEvent === 'showmap'){ //查看
						
								
							  } else if(layEvent === 'changedriver'){ //删除
							   layer.open({
								    type: 1,
								    title:'订单改派',
								    skin: 'layui-layer-rim', //加上边框
								    area: ['300px', '240px'], //宽高
								    content: `<div style='padding:10px'>
								    		<form class="am-form tpl-form-border-form" data-check="1" data-ajax="1">
								    		<div class='am-u-sm-12' style='margin-bottom:10px;font-size:12px'>
								    			订单号:${data1.order_no}
								    		</div>
								    		<div class='am-u-sm-12' style='margin-bottom:10px'>
								  				<input type="hidden" class="driver_id" name="driver_id"/>
												<input type="text" class="layui-input driver_name" style="line-height: 21px;" data-department="1"  placeholder="请选择司机" readonly="" >
								  			</div>
								  			<div class='am-u-sm-12' style='margin-bottom:10px'>
								  				<input type="hidden" class="car_id" name="car_id" value="" />
												<input type="text" class="layui-input  number" value="" placeholder="请选择车辆"  readonly=""/>
								  			</div>
								  			<div class='am-u-sm-12'>
								  				<button type='button' class='layui-btn layui-btn-sm change_sub'>提交</button>
								  			</div>
								  			</form>
								  			</div>`,
								  	success:function(){
								  		$('.driver_name').on('click',function(){
											opsearch("{:url('html/plus/driver')}?op=search")
										});
										$('.number').on('click',function(){
											opsearch("{:url('/html/car/car')}?op=search")
										});
										$('.change_sub').on('click',function(){
											var driver_id=$('.driver_id').val()
											
											if($('.driver_id').val()){
												if($('.car_id').val()){
													Load()
													ajax(api.order.redispatch,{
															car_number:$('.car_number').val(),
															driver_id:$('.driver_id').val(),
															car_id:$('.car_id').val(),
															order_id:data1.order_id
														},'post').done(function(){
															Load(1)
															console.log(res)
															if(res.code==1){
																Msg('改派成功')
																window.history.go(0)
															}else{
																Err(res.msg)
															}
														}).fail(function(){
															Load(1)
															Err('请求失败')
														})
													
												}else{
													Err('请选择车辆')
												}
											}else{
												Err('请选择司机')
											}
											
										});
								  	}
								});
							  } else if(layEvent === 'dispatch'){ //编辑
							 	 window.location.href= "{:url('/html/order/dispatch')}?order_id="+data1.order_id
							  } else if(layEvent === 'check'){ //编辑
								var auto = layer.open({
								type: 1,
								title:false,
								skin: 'layui-layer-rim', //加上边框
								area: ['200px', '100px'], //宽高
								content: `
									<div class='am-u-sm-12' style='padding:10px'>
										<div class='am-u-sm-12' style='text-align:center;margin-bottom:10px'>是否确认该订单</div>
										<div class='am-u-sm-12' style='text-align:center'>
											<a class='layui-btn layui-btn-sm layui-btn-primary am-radius check_ok'>审批</a>
											<a class='layui-btn layui-btn-sm layui-btn-primary am-radius close_no'>拒绝</a>
										</div>
									</div>
								`,
								success:function(){
									$('.check_ok').on('click',function(){
										Load()
										ajax(api.order.check,{
											order_id:data1.order_id,
											order_state:2,
										},'post').done((res)=>{
											Load(1)
												if(res.code==1){
													Msg('审批成功');
													window.history.go(0)
												}else{
													Err(res.msg)
												}
										}).fail(()=>{
											Load(1)
												Err('请求失败')
										})
										layer.close(auto)
									});
									$('.close_no').on('click',function(){
										layer.close(auto)
									})
								}
								})
							} 
						});
				}else{
					Err(res.msg)
				}
		}).fail(function(){
			Load(1)
				Err('请求失败')
		})	
	}